import React from 'react';
import RootToast from 'react-native-root-toast';
import { View, Text, Platform, StyleSheet } from 'react-native';

export default {

    toast: null,

    show: (msg) => {
        this.toast = RootToast.show(msg, {
            position: 0,
            duration: 2500
        });
    },

    showLong: (msg) => {
        this.toast = RootToast.show(msg, {
            position: 0,
            duration: 3000
        });
    },

    showSuccess: (msg, options) => {
        let toast = RootToast.show(
            Platform.OS === 'ios' ?
                <View style={styles.container}>
                    <Text style={styles.message}>{msg}</Text>
                </View> : msg, {
                duration: 2500,
                position: RootToast.positions.CENTER,
                ...options
            });
        setTimeout(function() {
            RootToast.hide(toast);
            typeof options === 'function' ? options && options() : null;
        }, 3000);
    },

    showLongSuccess: (msg, options) => {
        let toast = RootToast.show(
            Platform.OS === 'ios' ?
                <View style={styles.container}>
                    <Text style={styles.message}>{msg}</Text>
                </View> : msg, {
                duration: 3000,
                position: RootToast.positions.CENTER,
                ...options
            });
        setTimeout(function() {
            RootToast.hide(toast);
            typeof options === 'function' ? options && options() : null;
        }, 3500);
    },

    showWarning: (msg, options) => {
        let toast = RootToast.show(
            Platform.OS === 'ios' ?
                <View style={styles.container}>
                    <Text style={styles.message}>{msg}</Text>
                </View> : msg, {
                duration: RootToast.durations.SHORT,
                position: RootToast.positions.CENTER,
                ...options
            });
        setTimeout(function() {
            RootToast.hide(toast);
        }, RootToast.durations.SHORT + 500);
    },

    showError: (msg, options) => {
        let toast = RootToast.show(
            Platform.OS === 'ios' ?
                <View style={styles.container}>
                    <Text style={styles.message}>{msg}</Text>
                </View> : msg, {
                duration: RootToast.durations.SHORT,
                position: RootToast.positions.CENTER,
                ...options
            });
        setTimeout(function() {
            RootToast.hide(toast);
        }, RootToast.durations.SHORT + 500);
    }
};

const styles = StyleSheet.create({
    container: {
        width: 140,
        height: 120,
        alignItems: 'center',
        justifyContent: 'center'
    },
    message: {
        color: '#fff',
        marginTop: 10,
        textAlign: 'center',
        lineHeight: 20
    }
});
